Software transactional memory (STM) is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. A transaction in the context of transactional memory is a piece of code that executes a series of reads and writes to shared memory. A data value in the context of transactional memory is the particular segment of shared memory being accessed, such as a single object, a cache line (such as in C++), a page, a single word, etc. There are two broad types of concurrency control lock modes in transactional memory systems: optimistic and pessimistic.
With optimistic concurrency control, the system attempts to make forward progress at the risk that a conflict will be detected later on. The transactional memory system performs automatic resolution of such conflicts, often by rolling back one of the conflicting transactions and re-executing it. Optimistic operations are relatively inexpensive when compared to pessimistic operations since they just read and do not involve writes to shared locations (i.e. taking a lock). As the name implies, the hope for optimistic operations is that there are few conflicts. If this turns out to be false, then there will be already wasted work, and the system must then proceed to throw it away and attempt to resolve the conflict. This takes extra time.
With pessimistic concurrency control in transactional memory systems, the system ensures that forward progress can be made safely and conflict-free by doing more work up front to prevent conflicts. This involves locking shared memory by making one or more writes to a shared location (i.e. a lock). As the name implies, this is used for systems or pieces of code that exhibit above average conflict rates. Modern transactional memory systems select one or the other, which often results in some transactions not being executed in the manner best suited for the target workload.